package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.bo.SubjectItemExcel;
import com.tcm.entity.dto.param.SubjectItemPageParam;
import com.tcm.entity.dto.SubjectItemUpdateDto;
import com.tcm.entity.vo.BatchImportResult;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.SubjectItemListResult;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.SubjectItemService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/7/20 16:12
 */
@Slf4j
@RestController
@RequestMapping("/pc/item/subject")
public class SubjectItemController {

    @Autowired
    private SubjectItemService subjectItemService;

    /**
     * 添加主观题
     * @param dto 主观题更新数据封装
     * @param request http请求对象
     * @return 响应结果
     */
    @PostMapping
    public R addSubjectItem(@RequestBody @Valid SubjectItemUpdateDto dto, HttpServletRequest request){
        boolean b = false;
        try {
            b = subjectItemService.addSubjectItem(dto, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 修改主观题信息
     * @param dto 主观题更新数据封装对象
     * @param request http请求对象
     * @return 响应结果
     */
    @PutMapping
    public R modifySubjectItem(@RequestBody @Valid SubjectItemUpdateDto dto, HttpServletRequest request){
        if (dto.getId() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean b = false;
        try {
            b = subjectItemService.modifySubjectItem(dto, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 删除主观题
     * @param ids 主观题ID集合
     * @param request http请求对象
     * @return 响应结果
     */
    @DeleteMapping
    public R removeSubjectItem(@RequestParam("ids") List<Long> ids, HttpServletRequest request){
        if (CollectionUtils.isEmpty(ids)) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean b = false;
        try {
            b = subjectItemService.removeSubjectItem(ids, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 分页获取主观题列表
     * @param param 分页获取参数
     * @return 响应结果
     */
    @GetMapping
    public R getSubjectItemListByPage(SubjectItemPageParam param){
        if (param.getPage() == null || param.getPageSize() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        PageR<SubjectItemListResult> subjectItemListByPage = subjectItemService.getSubjectItemListByPage(param);
        return R.success(subjectItemListByPage);
    }

    /**
     * 批量导入主观题
     * @param file excel文件
     * @param request http请求对象
     * @return 导入校验结果
     */
    @PostMapping("/import")
    public R importSubjectItem(@RequestParam MultipartFile file, HttpServletRequest request){
        BatchImportResult<SubjectItemExcel> result = null;
        try {
            result = subjectItemService.importSubjectItem(file, AccessTokenUtil.getUserId(request));
        } catch (IOException e) {
            return R.fail(0, e.getMessage());
        }
        return R.success(result);
    }

    /**
     * 确认导入主观题
     * @param uuid 导入uuid
     * @param request http请求对象
     * @return 导入结果，true为成功
     * @throws DatabaseOperationException 数据库操作异常
     */
    @PutMapping("/import")
    public R confirmBatchClozeItem(String uuid, HttpServletRequest request){
        if (!StringUtils.hasText(uuid)){
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean b = false;
        try {
            b = subjectItemService.confirmBatchClozeItem(uuid, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.CACHE_NOT_EXIST);
    }

    /**
     * 导出校验失败主观题信息
     * @param uuid 导入uuid
     * @param response http响应对象
     * @return 导出校验失败主观题信息
     */
    @GetMapping("/import")
    public R exportImportFailedSubjectItem(String uuid, HttpServletResponse response){
        if (!StringUtils.hasText(uuid)){
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        try {
            subjectItemService.exportImportFailedSubjectItem(uuid, response);
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return null;
    }

    /**
     * 导出主观题信息
     * @param param 参数封装
     * @param response http响应对象
     * @return 导出文件
     */
    @GetMapping("/export")
    public R exportSubjectItem(SubjectItemPageParam param, HttpServletResponse response){
        try {
            subjectItemService.exportSubjectItem(param, response);
        } catch (IOException e) {
            return R.fail(0, e.getMessage());
        }
        return null;
    }

    /**
     * 根据IDs获取主观题信息
     * @param ids 主观题ID列表
     * @return 主观题信息列表
     */
    @GetMapping("/get")
    public R getSubjectItemByIds(@RequestParam("ids") List<Long> ids){
        if (CollectionUtils.isEmpty(ids)) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return R.success(subjectItemService.getSubjectItemByIds(ids));
    }

}
